package com.shundr.auth.common.view;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.TextView;

import com.shundr.auth.R;
import com.shundr.auth.activity.PreviewActivity;
import com.shundr.auth.common.util.PictureUtil;
import com.shundr.auth.frame.db.model.PictureInfo;
import com.shundr.auth.frame.util.StringUtil;

public class PicturePickDialog extends Dialog {

	private Activity mContext;
	private String  url;

	public PicturePickDialog(Activity context) {
		super(context);
		mContext = context;
	}
	
	public PicturePickDialog(Activity context, String url) {
		super(context);
		mContext = context;
		this.url = url;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		Window dialogwin = getWindow();
		dialogwin.requestFeature(Window.FEATURE_NO_TITLE);
		dialogwin.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
		dialogwin.setWindowAnimations(R.style.style_dialog_window_anim);

		setContentView(R.layout.dialog_pick_picture);

		TextView take_from_camera = (TextView) findViewById(R.id.user_auth_fail_description);
		TextView take_from_album = (TextView) findViewById(R.id.pic_album_take);
		TextView cancel = (TextView) findViewById(R.id.pic_cancel);
		TextView tv_preview = (TextView) findViewById(R.id.tv_preview);
		View v = findViewById(R.id.v_1);
		
		if (StringUtil.isNull(url)) {
			tv_preview.setVisibility(View.GONE);
			v.setVisibility(View.GONE);
		} else {
			tv_preview.setVisibility(View.VISIBLE);
			v.setVisibility(View.VISIBLE);
		}
		
		tv_preview.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (!StringUtil.isNull(url)) {
					Intent intnet = new Intent(mContext, PreviewActivity.class);
					intnet.putExtra("url", PicturePickDialog.this.url);
					mContext.startActivity(intnet);
					PicturePickDialog.this.dismiss();
				}
			}
		});

		take_from_camera.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				PictureUtil.doTakePhoto(mContext);
				PicturePickDialog.this.dismiss();
			}
		});

		take_from_album.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				PictureUtil.doPickPhotoFromGallery(mContext);

				PicturePickDialog.this.dismiss();
			}
		});

		cancel.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				PicturePickDialog.this.dismiss();
			}
		});

	}

	@Override
	public void show() {
		super.show();

		// set dialog width fill screen width
		WindowManager manager = mContext.getWindowManager();
		int width = manager.getDefaultDisplay().getWidth();

		Window dialogwin = getWindow();

		LayoutParams lp = dialogwin.getAttributes();
		lp.width = width;
		dialogwin.setAttributes(lp);
	}

}
